﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Beeldbankmvc.Models;
using System.Configuration;

namespace Beeldbankmvc.Helpers
{
    public class DynamicContent
    {
        public static string getTitle(String path)
        {
            PageTextRepository pageTextRepository = new PageTextRepository();
            String publicationName = ConfigurationManager.AppSettings["publicationName"];

            // To process 
            // URL's that end with slash
            // http://localhost/BeeldbankRuimteVoorDeRivier/
            // exept for the root "/"
            if (path.EndsWith("/") && !path.Equals("/"))
            {
                path = path.Substring(0, path.LastIndexOf("/"));
            }

            // To process
            // "/Account/EnhancedAccount/95d5c5cf-b0b2-44f1-8097-865e649d256e" properly
            // we need a workaround
            if (path.Contains("-"))
            {
                path = path.Substring(0, path.LastIndexOf("/"));
            }
            // To process
            // "/ShoppingCart/CheckOut?i=1" properly
            // we need a workaround
            if (path.Contains("="))
            {
                path = path.Substring(0, path.LastIndexOf("?"));
            }
            var PageTextItem = pageTextRepository.FindOne(path, publicationName);

            return PageTextItem.Title;
        }

        public static string getBodyText(String path)
        {
            PageTextRepository pageTextRepository = new PageTextRepository();
            String publicationName = ConfigurationManager.AppSettings["publicationName"];


            // To process 
            // URL's that end with slash
            // http://localhost/BeeldbankRuimteVoorDeRivier/
            // exept for the root "/"
            if (path.EndsWith("/") && !path.Equals("/"))
            {
                path = path.Substring(0, path.LastIndexOf("/"));
            }

            // To process
            // "/Account/EnhancedAccount/95d5c5cf-b0b2-44f1-8097-865e649d256e" properly
            // we need a workaround
            if (path.Contains("-"))
            {
                path = path.Substring(0, path.LastIndexOf("/"));
            }
            // To process
            // "/ShoppingCart/CheckOut?i=1" properly
            // we need a workaround
            if (path.Contains("="))
            {
                path = path.Substring(0, path.LastIndexOf("?"));
            }
            var PageTextItem = pageTextRepository.FindOne(path, publicationName);

            return PageTextItem.Bodytext;
        }
    }
}